package com.ajiao4.Demo;

import com.util.JDBCUtil;
import java.lang.reflect.Field;
import java.sql.*;
import java.util.Scanner;

public class Exer2Test {
    /**
     *     问题：1向examstudent添加一条记录
     */
//    public static void main(String[] args) {
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("四级/六级");
//        int type = scanner.nextInt();
//        System.out.print("身份证号：");
//        String IDCard = scanner.next();
//        System.out.print("准考证号：");
//        String examCard = scanner.next();
//        System.out.print("学生姓名：");
//        String studentName = scanner.next();
//        System.out.print("所在城市：");
//        String location = scanner.next();
//        System.out.print("考试成绩:");
//        int grade = scanner.nextInt();
//
//        String sql = "insert into examstudent(Type,IDCard,ExamCard,StudentName,Location,Grade)values(?,?,?,?,?,?)";
//        int insertCount = update(sql, type, IDCard, examCard, studentName, location, grade);
//        if (insertCount >= 1) {
//            System.out.println("添加成功");
//        } else {
//            System.out.println("添加失败");
//        }
//
//    }

    /**
     * 通用的增删改操作
     */
    //sql语句中的占位符的个数与可变形参的长度相同
    public static int update(String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        try {
            //1.获取连接
            conn = JDBCUtil.getConnection();
            //2.预编译,返回preparedStatement的实例
            ps = conn.prepareStatement(sql);
            //3.填充占位符
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);//占位符是从1开始的 所以i+1
            }
            //4.执行  如果执行的是查询操作，有返回结果，则返回true。
            //如果执行的是增删改操作，没有返回结果，则方法返回false
            return ps.executeUpdate();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            //5.关闭资源
            JDBCUtil.CloseResource(conn, ps);
        }
        return 0;
    }


    /**
     * 问题2：根据身份号或者准考证号查询
     */
    public static void main(String[] args) {
        Exer2Test exer2Test = new Exer2Test();
        exer2Test.queryWithIDCardOrExamCard();
    }

    public void queryWithIDCardOrExamCard() {
        System.out.println("请选择您要输入的类型:");
        System.out.println("a.准考证号");
        System.out.println("b.身份证号");
        Scanner scanner = new Scanner(System.in);
        String selection = scanner.next();
        if ("a".equalsIgnoreCase(selection)) {
            System.out.println("请输入准考证号:");
            String examCard = scanner.next();
            String sql = "select FlowID flowID,Type type,IDCard,ExamCard examCard,StudentName studentName,Location location,Grade grade from examstudent WHERE FlowID = ?";
            Student student = GetInstance(Student.class, sql, examCard);
            if (student != null) {
                System.out.println(student);
            } else {
                System.out.println("输入的准考证号有误");
            }
        } else if ("b".equalsIgnoreCase(selection)) {
            System.out.println("请输入身份证:");
            String IDCard = scanner.next();
            String sql = "select FlowID flowID,Type type,IDCard,ExamCard examCard,StudentName studentName,Location location,Grade grade from examstudent WHERE IDCard = ?";
            Student student = GetInstance(Student.class, sql, IDCard);
            if(student!=null)
            {
                System.out.println(student);
            }else {
                System.out.println("输入的身份证号有误");
            }

        } else {
            System.out.println("您输入的有误，请重新输入。");
        }
    }

    public <T> T GetInstance(Class<T> clazz, String sql, Object... args) {
        Connection conn = null;
        PreparedStatement ps = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtil.getConnection();
            ps = conn.prepareStatement(sql);
            for (int i = 0; i < args.length; i++) {
                ps.setObject(i + 1, args[i]);
            }

            //执行
            rs = ps.executeQuery();

            //获取结果集的元数据
            ResultSetMetaData rsmd = rs.getMetaData();

            //获取列数
            int columnCount = rsmd.getColumnCount();

            if (rs.next()) {

                T t = clazz.newInstance();
                for (int i = 0; i < columnCount; i++) {
                    //获取每个列的值:通过结果集ResultSet
                    Object value = rs.getObject(i + 1);

                    //通过元数据ResultSetMetaData
                    //只能获取列的列名getColumnName
                    //获取列的别名getColumnLabel,如果sql没有设置别名，默认的拿到列名
//                    String columnName = rsmd.getColumnName();
                    String countLabel = rsmd.getColumnLabel(i + 1);

                    //给cust对象指定的columnName属性，赋值为value，通过反射
                    //通过反射去找Customer类中对应columnName列名的属性，然后给他复制columnvalue
                    Field field = clazz.getDeclaredField(countLabel);
                    field.setAccessible(true);
                    field.set(t, value);
                }
                return t;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.CloseResource(conn, ps, rs);
        }
        return null;
    }
}
